package com.ctsi.ntedu.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctsi.ntedu.service.NteduSchoolService;
import com.ctsi.ntedu.domain.NteduSchool;
import com.ctsi.ntedu.domain.NteduSchoolExample;

import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing NteduSchool.
 *
 * @author ctsi-biyi-generator
 *
 */
@RestController
@RequestMapping("/api")
public class NteduSchoolController {

    private final Logger log = LoggerFactory.getLogger(NteduSchoolController.class);

    private static final String ENTITY_NAME = "nteduSchool";

    private final NteduSchoolService nteduSchoolService;

    public NteduSchoolController(NteduSchoolService nteduSchoolService) {
        this.nteduSchoolService = nteduSchoolService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /nteduSchools : Create a new nteduSchool.
     *
     * @param nteduSchool the nteduSchool to create
     * @return the ResponseEntity with status 201 (Created) and with body the new nteduSchool, or with status 400 (Bad Request) if the nteduSchool has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/nteduSchools")
    public ResponseEntity<NteduSchool> createNteduSchool(@RequestBody NteduSchool nteduSchool) throws URISyntaxException {
        log.debug("REST request to save NteduSchool : {}", nteduSchool);
        NteduSchool result = nteduSchoolService.insert(nteduSchool);
        return ResponseEntity.created(new URI("/api/nteduSchools" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /nteduSchools : Updates an existing nteduSchool.
     *
     * @param nteduSchool the nteduSchool to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated nteduSchool,
     * or with status 400 (Bad Request) if the nteduSchool is not valid,
     * or with status 500 (Internal Server Error) if the nteduSchool couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/nteduSchools")
    public ResponseEntity<NteduSchool> updateNteduSchool(@RequestBody NteduSchool nteduSchool)  {
        log.debug("REST request to update NteduSchool : {}", nteduSchool);
        NteduSchool result = nteduSchoolService.update(nteduSchool);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
    
    /**
     * GET  /nteduSchools : get the nteduSchools with page.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of nteduSchools in body
     */
    @GetMapping("/nteduSchoolsByCriteria")
    public PageResult<NteduSchool> getNteduSchoolsByCriteria(NteduSchoolExample nteduSchoolExample, Pageable page) {
        log.debug("REST request to get NteduSchoolsByCriteria");
        return nteduSchoolService.findByExample(nteduSchoolExample, page);
    }
    
    /**
     * GET  /nteduSchools : get the nteduSchools.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of nteduSchools in body
     */
    @GetMapping("/nteduSchoolsList")
    public PageResult<NteduSchool> getNteduSchoolsList(NteduSchoolExample nteduSchoolExample) {
        log.debug("REST request to get NteduSchoolsList");
        return nteduSchoolService.findByExample(nteduSchoolExample);
    }

    /**
     * GET  /nteduSchools/:id : get the "id" nteduSchool.
     *
     * @param id the id of the nteduSchool to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the nteduSchool, or with status 404 (Not Found)
     */
    @GetMapping("/nteduSchools/{id}")
    public ResponseEntity<NteduSchool> getNteduSchool(@PathVariable Integer id) {
        log.debug("REST request to get NteduSchool : {}", id);
        NteduSchool nteduSchool = nteduSchoolService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(nteduSchool));
    }
	
    /**
     * DELETE  /nteduSchools/:id : delete the "id" nteduSchool.
     *
     * @param id the id of the nteduSchool to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/nteduSchools/{id}")
    public ResponseEntity<Void> deleteNteduSchool(@PathVariable Integer id) {
        log.debug("REST request to delete NteduSchool : {}", id);
        nteduSchoolService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

//    @GetMapping("/nteduSchools/tel")
//    public PageResult<NteduSchool> getNteduSchoolsTelList(NteduSchoolExample nteduSchoolExample){
//        log.debug("Rest request to show calling case : {}");
//    }
    
}
